---
title: "Python getopt"
date: 2017-6-30
categories:
- python
tags:
---

<div id="content">
<div class="org-src-container">
<pre class="src src-python"><span style="font-weight: bold;">import</span> sys, getopt

<span style="font-weight: bold;">def</span> <span style="font-weight: bold;">main</span>(argv):
        <span style="font-weight: bold;">try</span>:
                <span style="font-weight: bold; font-style: italic;">opts</span>, <span style="font-weight: bold; font-style: italic;">args</span> = getopt.getopt(argv, <span style="font-style: italic;">"hvh:p:"</span>, [<span style="font-style: italic;">"help"</span>, <span style="font-style: italic;">"version"</span>, <span style="font-style: italic;">"host="</span>, <span style="font-style: italic;">"port="</span>])
        <span style="font-weight: bold;">except</span> getopt.GetoptError <span style="font-weight: bold;">as</span> err:
                <span style="font-weight: bold;">print</span>(<span style="font-style: italic;">"getopt error"</span>)
                <span style="font-weight: bold;">print</span>(error)
                sys.<span style="font-weight: bold; text-decoration: underline;">exit</span>(1)

        <span style="font-weight: bold;">for</span> opt, arg <span style="font-weight: bold;">in</span> opts:
                <span style="font-weight: bold;">if</span> opt <span style="font-weight: bold;">in</span> (<span style="font-style: italic;">"-h"</span>, <span style="font-style: italic;">"--help"</span>):
                        <span style="font-weight: bold;">print</span>(<span style="font-style: italic;">"help"</span>)
                <span style="font-weight: bold;">elif</span> opt <span style="font-weight: bold;">in</span> (<span style="font-style: italic;">"-v"</span>, <span style="font-style: italic;">"--version"</span>):
                        <span style="font-weight: bold;">print</span>(<span style="font-style: italic;">"version"</span>)
                <span style="font-weight: bold;">elif</span> opt <span style="font-weight: bold;">in</span> (<span style="font-style: italic;">"-h"</span>, <span style="font-style: italic;">"--host"</span>):
                        <span style="font-weight: bold;">print</span>(<span style="font-style: italic;">"host"</span>)
                <span style="font-weight: bold;">elif</span> opt <span style="font-weight: bold;">in</span> (<span style="font-style: italic;">"-p"</span>, <span style="font-style: italic;">"--port"</span>):
                        <span style="font-weight: bold;">print</span>(<span style="font-style: italic;">"port"</span>)

<span style="font-weight: bold;">if</span> <span style="font-weight: bold;">__name__</span> == <span style="font-style: italic;">"__main__"</span>:
        main(sys.argv[1:])

</pre>
</div>
</div>
<div class="status" id="postamble">
<p class="date">Date: 2017-6-30</p>
<p class="author">Author: gdme1320</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
